<?php

namespace App\Http\Controllers\Notify;

use App\Http\Controllers\Controller;
use App\Models\Purchase\PurchasePayOrder;
use App\Models\Purchase\PurchasePayOrderNew;
use App\Models\Purchase\PurchasePrePayOrder;
use App\Services\Balance\Balance;
use App\Services\Common\SnCode;
use Illuminate\Http\Request;

use App\Services\DingDing\DingCallbackCrypto;
use Illuminate\Support\Facades\Log;

class DingDingController extends Controller
{
    /**
     * 钉钉异步回调
     */
    public function callback(Request $request){

        $appKey = env('DINGDING_APPKEY');
        $token = env('DINGDING_TOKEN');
        $aesKey = env('DINGDING_AESKEY');


        $signature = $request->signature;
        $timeStamp = $request->timestamp;
        $nonce = $request->nonce;
        $postdata = file_get_contents("php://input");


        Log::info($postdata);
        $postList = json_decode($postdata, true);
        $encrypt = $postList['encrypt'];

        $crypt = new DingCallbackCrypto($token, $aesKey, $appKey);
        $text = $crypt->getDecryptMsg($signature, $timeStamp, $nonce, $encrypt);
        Log::info($text);
        $eventMsg = json_decode($text, true);

        if(isset($eventMsg['EventType']) && $eventMsg['EventType']=='check_url'){
            $res = $crypt->getEncryptedMap("success");
            return response()->json(json_decode($res));
        }

        $EventType=isset($eventMsg['EventType'])?$eventMsg['EventType']:'';

        if($EventType=='bpms_task_change'){
            $type='';

            if($type=='finish'){
                $this->payordernewcallback($request);
            }
        }


        

        
    }

    /**
     * 付款单审核新
     */
    protected function payordernewcallback(Request $request){
        //根据异步回调信息得到审批实例id以及结果
        $processinstanceid='';
        $result='agree';
        $info = PurchasePayOrderNew::where(['is_del' => 1, 'processinstanceid' => $processinstanceid])->first();
        if (!$info) {
            return false;
        }
        //更新审核状态
        if($result=='agree'){

            $audit_status=3;
        }else{
            $audit_status=4;
        }

        PurchasePayOrderNew::where(['is_del' => 1, 'processinstanceid' => $processinstanceid])->update(['audit_status'=>$audit_status,'update_at'=>time()]);

    }
    /**
     * 预付单审核
     */
    protected function prepayordercallback(Request $request){
        $id='';
        $info = PurchasePrePayOrder::where(['is_del' => 1, 'id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }  
        $info=$info->toArray();

        $cpid=$info['cpid'];
        $manger_id=$info['manger_id'];

        $accountname='';
        $data=[
            'cpid'=>$cpid,
            'manger_id'=>$manger_id,
            'pa_type'=>$info['pa_type'],
            'order_id'=>$info['order_id'],
            'accountname'=>$accountname,
            'pay_money'=>$info['pay_money'],
            'custom_id'=>$info['custom_id'],
            'company_name'=>$info['company_name'],
            'po_order_sn_code'=>$info['po_order_sn_code'],
            'order_sn_code'=>SnCode::getSnCode('PPO_'),
            'order_date'=>time(),
            'create_at'=>time(),
            'update_at'=>time()
        ];

        PurchasePayOrder::create($data);//写入付款单
        
        // Balance::purchaseRecharge($info['custom_id'],$cpid,$info['pay_money']);




    } 
}